﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ProjBackEnd.Areas.BizProj.Models;
using ProjBackEnd.Lib;
using ProjBackEnd.DAL;

namespace ProjBackEnd.Areas.BizProj.Controllers
{
    /// <summary>
    /// 项目明细操作
    /// </summary>
    public class ProjDtlOptController : ApiController
    {
        /// <summary>
        /// 获取项目明细列表
        /// eg. : 0000000001|TST1|WEB|1|0|0|0
        /// </summary>
        /// <param name="key">memberid|comcd|平台CD|项目编号|合同id（0=所有）|合同子项id（0=所有）|项目明细编号（0=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/bizproj/projdtlopt")]
        public RespProjDtl GetProjDtl(string key)
        {
            RespProjDtl resp = new RespProjDtl();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 7)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            int iProjid,iCtid,iCtSubid, iProjdtlid;
            if (!int.TryParse(strsKeyOriginal[3], out iProjid)
                || !int.TryParse(strsKeyOriginal[4], out iCtid)
                || !int.TryParse(strsKeyOriginal[5], out iCtSubid)
                || !int.TryParse(strsKeyOriginal[6], out iProjdtlid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (iProjid == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "项目编号不能为 0";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.GetProjDtlList(iProjid, iCtid, iCtSubid, iProjdtlid);
            return resp;
        }

        /// <summary>
        /// 维护项目明细常规信息
        /// </summary>
        /// <param name="_reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizproj/projdtlopt")]
        public RespProjNullResultInfo UpdateProjDtl(ReqProjDtl _reqInfo)
        {
            RespProjNullResultInfo resp = new RespProjNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.UpdateProjDtlInfo(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.projdtlinfo);
            return resp;
        }

        /// <summary>
        /// 将项目明细转移至指定的合同子项
        /// 可一次转移多项，但必须归属于同一个合同子项
        /// </summary>
        /// <param name="_reqInfo">明细转移请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizproj/projdtltrans")]
        public RespProjNullResultInfo ProjDtlTransfer(ReqProjDtlTrans _reqInfo)
        {
            RespProjNullResultInfo resp = new RespProjNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.ProjDtlTransfer(_reqInfo.ctsubid_target, _reqInfo.ctsubid_sour, _reqInfo.dtlidlist);
            return resp;
        }

    }
}
